unit UContasPagarFt;

interface

uses Forms, UContasPagarCt, SysUtils, URequestContasPagar;

type
  TContasPagarFt = class(TObject)

    private

    public
      class procedure service(req : TRequestContasPagar);
      class function obterControle(req : TRequestContasPagar) : TContasPagarCt;
      class procedure verificarAcaoRequest(req : TRequestContasPagar; pObjContasPagarCt : TContasPagarCt);

end;

implementation

uses UContasPagarMd, FContasPagarFt, DB, DBGrids, FCrudContasPagar;

class procedure TContasPagarFt.service(req : TRequestContasPagar);
var
  controlador : TContasPagarCt;
begin
  controlador := self.obterControle(req);
  self.verificarAcaoRequest(req, controlador);
end;

class procedure TContasPagarFt.verificarAcaoRequest(req : TRequestContasPagar;  pObjContasPagarCt : TContasPagarCt);

begin
  if (req.acao = 'alterar') then
  begin
    with TFCrudContasPagarFt.Create(nil) do
    begin
      botaoSalvar.Visible := false;
      botaoAlterar.Visible := true;

      txtIdContasPagar.Text := IntToStr(req.id_contasPagar);
      txtDataPagamento.Date := req.data_pagamento;
      txtValor.Text := FormatFloat('0.,00', req.valor);
      txtObservacoes.Lines.Text := req.observacoes;

      ShowModal;
      Free;
    end;
  end;

  if (req.acao = 'novo') then
  begin
    with TFCrudContasPagarFt.Create(nil) do
    begin
      botaoSalvar.Visible := true;
      botaoAlterar.Visible := false;
      ShowModal;
      Free;
    end;
  end;

  if (req.acao = 'inserir') then
  begin
    pObjContasPagarCt.inserirValorContasPagar(req);
    pObjContasPagarCt.inserirContasPagar;
  end;

  if (req.acao = 'atualizar') then
  begin
    pObjContasPagarCt.inserirValorContasPagar(req);
    pObjContasPagarCt.alterarContasPagar;  
  end;

  if (req.acao = 'excluir') then
  begin
    pObjContasPagarCt.excluirContasPagar(req)
  end;

  if (req.acao = 'exibir') then
  begin
    with TFCrudContasPagarFt.Create(nil) do
    begin
      botaoSalvar.Visible := false;
      botaoAlterar.Visible := false;

      txtIdContasPagar.Text := IntToStr(req.id_contasPagar);
      txtDataPagamento.Date := req.data_pagamento;
      txtValor.Text := FormatFloat('0.,00', req.valor);
      txtObservacoes.Lines.Text := req.observacoes;

      txtIdContasPagar.Enabled := false;
      txtDataPagamento.Enabled := false;
      txtValor.Enabled := false;
      txtObservacoes.Enabled := false;

      ShowModal;

      txtIdContasPagar.Enabled := true;
      txtDataPagamento.Enabled := true;
      txtValor.Enabled := true;
      txtObservacoes.Enabled := true;

      Free;
    end;  
  end;

  if (req.acao = 'pagar') then
  begin
      pObjContasPagarCt.pagarContasPagar(req);
  end;

end;

class function TContasPagarFt.obterControle(req : TRequestContasPagar) : TContasPagarCt;
var
  lObjContasPagarCt : TContasPagarCt;
begin
  if (req.objContasPagarCt = nil) then
  begin
    lObjContasPagarCt := TContasPagarCt.Create;
    TContasPagarCt(req.objContasPagarCt) := lObjContasPagarCt;
  end
  else
    lObjContasPagarCt := TContasPagarCt(TFContasPagarFt(req.objContasPagarCt));

  result := lObjContasPagarCt;
end;

end.
